#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

//MSB数据地址共14字节ACCEL_X、ACCEL_Y、ACCEL_Z、TEMP、GYRO_X、GYRO_Y、GYRO_Z每个两字节
int main(int argc, char **argv)
{
	int fd=0;
	int ret=0;
	int i=0;
	unsigned char data[14]={0};
	short mpu6050Data[7] = {0};

	if(argc !=2)
	{
		printf("Usage error %s\r\n",argv[0]);
		return -1;
	}

	fd = open(argv[1],O_RDWR);
	if(fd <0)
	{
		printf("open file:%s error\r\n",argv[1]);
		return -1;
	}
	sleep(1);

	while(1)
	{
		ret = read(fd, data, sizeof(data));
		if(ret <0)
		{
			printf("read error! ret:%d\r\n",ret);
			return -1;
		}

		mpu6050Data[0] = (data[0]<<8) +data[1];
		mpu6050Data[1] = (data[2]<<8) +data[3];
		mpu6050Data[2] = (data[4]<<8) +data[5];
		mpu6050Data[3] = (data[6]<<8) +data[7];
		mpu6050Data[4] = (data[8]<<8) +data[9];
		mpu6050Data[5] = (data[10]<<8) +data[11];
		mpu6050Data[6] = (data[12]<<8) +data[13];

		printf("ACCEL_X:%hd\r\n",mpu6050Data[0]);
		printf("ACCEL_Y:%hd\r\n",mpu6050Data[1]);
		printf("ACCEL_Z:%hd\r\n",mpu6050Data[2]);
		printf("TEMP:%02.1f\r\n",mpu6050Data[3]/340.0+36.53);
		printf("GYRO_X:%hd\r\n",mpu6050Data[4]);
		printf("GYRO_Y:%hd\r\n",mpu6050Data[5]);
		printf("GYRO_Z:%hd\r\n",mpu6050Data[6]);
		printf("\r\n");
		
		sleep(1);
	}
	
	close(fd);

	return 0;
}


